package cn.gemiman.daoyi.module.cms.controller.app.appsite;

import cn.gemiman.daoyi.framework.common.enums.CommonStatusEnum;
import cn.gemiman.daoyi.framework.common.enums.UserTypeEnum;
import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import cn.gemiman.daoyi.framework.security.core.annotations.PreAuthenticated;
import cn.gemiman.daoyi.framework.security.core.util.SecurityFrameworkUtils;
import cn.gemiman.daoyi.framework.web.core.util.WebFrameworkUtils;
import cn.gemiman.daoyi.module.cms.controller.admin.appsite.vo.AppSitePageReqVO;
import cn.gemiman.daoyi.module.cms.controller.admin.appsite.vo.AppSiteRespVO;
import cn.gemiman.daoyi.module.cms.controller.admin.appsite.vo.AppSiteSaveReqVO;
import cn.gemiman.daoyi.module.cms.controller.admin.appsitefootprint.vo.AppSiteFootprintPageReqVO;
import cn.gemiman.daoyi.module.cms.controller.admin.appsitefootprint.vo.AppSiteFootprintRespVO;
import cn.gemiman.daoyi.module.cms.controller.admin.appsitefootprint.vo.AppSiteFootprintSaveReqVO;
import cn.gemiman.daoyi.module.cms.controller.app.appsite.vo.AppAppSiteFriendChainRespVO;
import cn.gemiman.daoyi.module.cms.controller.app.appsite.vo.AppAppSiteSaveReqVO;
import cn.gemiman.daoyi.module.cms.controller.app.appsite.vo.AppAppSiteStatisticRespVO;
import cn.gemiman.daoyi.module.cms.dal.dataobject.appsite.AppSiteDO;
import cn.gemiman.daoyi.module.cms.dal.dataobject.appsitefootprint.AppSiteFootprintDO;
import cn.gemiman.daoyi.module.cms.service.appsite.AppSiteService;
import cn.gemiman.daoyi.module.cms.service.appsitefootprint.AppSiteFootprintService;
import cn.gemiman.daoyi.module.cms.service.tagdictionary.TagDictionaryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 站点")
@RestController
@RequestMapping("/cms/app-site")
@Validated
public class AppAppSiteController {

    @Resource
    private AppSiteService appSiteService;
    @Resource
    private AppSiteFootprintService appSiteFootprintService;
    @Resource
    private TagDictionaryService tagDictionaryService;

    @PostMapping("/applyForFriendChain")
    @Operation(summary = "申请友链")
    @PreAuthenticated
    public CommonResult<Long> applyForFriendChain(@Valid @RequestBody AppAppSiteSaveReqVO createReqVO) {
        AppSiteSaveReqVO bean = BeanUtils.toBean(createReqVO, AppSiteSaveReqVO.class);
        return success(appSiteService.createAppSite(bean));
    }

    @GetMapping("/pageForFriendChain")
    @Operation(summary = "获得友链分页")
    public CommonResult<PageResult<AppAppSiteFriendChainRespVO>> pageForFriendChain(@Valid AppSitePageReqVO pageReqVO) {
        pageReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        PageResult<AppSiteDO> pageResult = appSiteService.getAppSitePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppAppSiteFriendChainRespVO.class));
    }


    @PostMapping("/addFootprint")
    @Operation(summary = "添加足迹")
    @PreAuthenticated
    public CommonResult<Long> addFootprint(@Valid @RequestBody AppSiteFootprintSaveReqVO createReqVO) {
        String appId = WebFrameworkUtils.getAppId();
        AppSiteDO appSite = appSiteService.getAppSite(appId);
        createReqVO.setAppId(appId).setAppSiteId(appSite.getId());
        createReqVO.setUserId(SecurityFrameworkUtils.getLoginUserId()).setUserType(UserTypeEnum.MEMBER.getValue());
        return success(appSiteFootprintService.addFootprint(createReqVO));
    }

    @GetMapping("/getFootprint")
    @Operation(summary = "获得站点足迹")
    @PreAuthenticated
    public CommonResult<AppSiteFootprintRespVO> getAppSiteFootprintPage(@Valid AppSiteFootprintPageReqVO pageReqVO) {
        String appId = WebFrameworkUtils.getAppId();
        AppSiteDO appSite = appSiteService.getAppSite(appId);
        pageReqVO.setAppId(appId).setAppSiteId(appSite.getId());
        pageReqVO.setUserId(SecurityFrameworkUtils.getLoginUserId()).setUserType(UserTypeEnum.MEMBER.getValue());
        PageResult<AppSiteFootprintDO> pageResult = appSiteFootprintService.getAppSiteFootprintPage(pageReqVO);
        if (pageResult.getList().isEmpty()) {
            return success(new AppSiteFootprintRespVO());
        }
        return success(BeanUtils.toBean(pageResult.getList().getFirst(), AppSiteFootprintRespVO.class));
    }

    @GetMapping("/get")
    @Operation(summary = "获得站点")
    public CommonResult<AppSiteRespVO> getAppSite() {
        String appId = WebFrameworkUtils.getAppId();
        AppSiteDO appSite = appSiteService.getAppSite(appId);
        return success(BeanUtils.toBean(appSite, AppSiteRespVO.class));
    }

    @PutMapping("/addView")
    @Operation(summary = "登记访问量")
    public CommonResult<Long> addAppSiteView() {
        return success(appSiteService.addAppSiteView(WebFrameworkUtils.getAppId()));
    }

    @GetMapping("/getTagDictionary")
    @Operation(summary = "获取标签")
    public CommonResult<List<String>> getTagDictionary() {
        return success(tagDictionaryService.getTagDictionary());
    }

    @GetMapping("/statistic")
    @Operation(summary = "获取数据统计")
    public CommonResult<AppAppSiteStatisticRespVO> getStatistic() {
        return success(appSiteService.getStatistic(WebFrameworkUtils.getAppId()));
    }
}
